package wnhz.com.business.groupshop.ui;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import wnhz.com.business.MyApplication;
import wnhz.com.business.R;
import wnhz.com.business.base.BaseActivity;
import wnhz.com.business.constants.Constants;
import wnhz.com.business.groupshop.bean.AddressInfo;
import wnhz.com.business.uitls.Base64Util;
import wnhz.com.business.uitls.BroadCastReceiverUtil;
import wnhz.com.business.uitls.dialog.SimpleDialog;
import wnhz.com.business.impl.ActionBarClickListener;
import wnhz.com.business.uitls.itemremoverecyclerview.AddressAdapter;
import wnhz.com.business.uitls.itemremoverecyclerview.ItemAddressRemoveRecyclerView;
import wnhz.com.business.uitls.itemremoverecyclerview.OnItemClickListener;
import wnhz.com.business.uitls.LogUtils;
import wnhz.com.business.uitls.MyCallBack;
import wnhz.com.business.uitls.ToastUtils;
import wnhz.com.business.uitls.Url;
import wnhz.com.business.uitls.XUtil;
import wnhz.com.business.view.TranslucentActionBar;

public class AddressActivity extends BaseActivity implements ActionBarClickListener, AddressAdapter.EditClick,AddressAdapter.MOrenClick {
    private static final String TAG = "AddressActivity";

    @BindView(R.id.id_item_remove_recyclerview)
    ItemAddressRemoveRecyclerView recyclerView;
    @BindView(R.id.actionbar)
    TranslucentActionBar actionbar;
    @BindView(R.id.empty_layout)
    LinearLayout empty_layout;
    @BindView(R.id.tv_msg)
    TextView tv_msg;

    private SimpleDialog simpleDialog;
    private List<AddressInfo.InfoBean> addressInfoList = new ArrayList<>();
    private Map<String, Object> map = new HashMap<>();
    private String from;
    private AddressAdapter adapter;
    private AddressInfo.InfoBean infoBean;

    public static Intent createIntent(Context context, String from) {
        Intent intent = new Intent(context, AddressActivity.class);
        intent.putExtra("from", from);
        return intent;
    }

    public void parseIntent() {
        Intent intent = getIntent();
        from = intent.getStringExtra("from");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_adress);
        ButterKnife.bind(this);
        parseIntent();
        actionbar.setData("选择地址", R.drawable.ic_lefft_white, null, 0, null,0, this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            actionbar.setStatusBarHeight(getStatusBarHeight());
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initAddressList();
    }

    private void initAddressList() {
        showDialog("加载中...");
        map.put("token", MyApplication.getToken());

        XUtil.Post(Url.GET_ADDRESSLIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                LogUtils.e("===获取地址列表===", result);

                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if (jsonObject.optString("re").equals("1")) {

                        Gson gson = new Gson();
                        AddressInfo addressInfo = gson.fromJson(result, AddressInfo.class);
                        addressInfoList = addressInfo.getInfo();
                        recyclerView.setVisibility(View.VISIBLE);
                        empty_layout.setVisibility(View.GONE);
                        initItemRectclerView();

                    } else if (jsonObject.optString("re").equals("0")) {
                        recyclerView.setVisibility(View.GONE);
                        empty_layout.setVisibility(View.VISIBLE);
                        tv_msg.setText("暂无收货地址~");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }
        });
    }

    @OnClick(R.id.ll_new)
    public void onNewClick() {
        startActivity(AddNewAddressActivity.createIntent(AddressActivity.this));
    }

    private void initItemRectclerView() {
        adapter = new AddressAdapter(this, addressInfoList);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter.setEditClickListener(this);
        adapter.SetMOrenClickLisener(this);
        recyclerView.setAdapter(adapter);
        recyclerView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (from.equals("1") || from.equals("2")) {
                    final AddressInfo.InfoBean infoBean = (AddressInfo.InfoBean) adapter.getData(position);
                    Intent intent = new Intent();
                    intent.putExtra("addressBean", infoBean);
                    setResult(RESULT_OK, intent);
                    finish();
                }
            }

            @Override
            public void onDeleteClick(final int position) {
                //删除按钮
                final AddressInfo.InfoBean infoBean = (AddressInfo.InfoBean) adapter.getData(position);

                simpleDialog = new SimpleDialog(AddressActivity.this, "确定删除收货地址？", "再想想", "删除", new SimpleDialog.OnButtonClick() {
                    @Override
                    public void onNegBtnClick() {
                        simpleDialog.cancel();
                    }

                    @Override
                    public void onPosBtnClick() {
                        map.clear();
                        map.put("token", MyApplication.getToken());
                        map.put("aid", infoBean.getAid());
                        map.put("type", 3); //删除

                        XUtil.Post(Url.ADD_NEW_ADDRESS, map, new MyCallBack<String>() {
                            @Override
                            public void onSuccess(String result) {
                                super.onSuccess(result);
                                LogUtils.e("==删除地址=", result);
                                try {
                                    JSONObject jsonObject = new JSONObject(result);

                                    if (jsonObject.optString("re").equals("1")) {
                                        adapter.removeItem(position);
                                    }

                                    initDeleteAddressList();
                                    ToastUtils.showToast(AddressActivity.this, jsonObject.optString("info"));
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }

                            @Override
                            public void onFinished() {
                                super.onFinished();
                                closeDialog();
                            }

                            @Override
                            public void onError(Throwable ex, boolean isOnCallback) {
                                super.onError(ex, isOnCallback);
                                ex.printStackTrace();
                            }
                        });
                    }
                });
                simpleDialog.show();
                return;
            }

            @Override
            public void onDefaultClick(int position) {
                //编辑按钮
                final AddressInfo.InfoBean infoBean = (AddressInfo.InfoBean) adapter.getData(position);
                startActivity(EditAddressActivity.createIntent(AddressActivity.this, infoBean));
            }
        });
    }

    @Override
    public void onLeftClick() {
        finish();
    }

    @Override
    public void onRightClick() {

    }

    @Override
    public void onEditTextClick() {

    }

    private void initDeleteAddressList() {
        showDialog("删除中...");
        map.put("token", MyApplication.getToken());

        XUtil.Post(Url.GET_ADDRESSLIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                LogUtils.e("===获取地址列表===", result);

                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if (jsonObject.optString("re").equals("1")) {

                        Gson gson = new Gson();
                        AddressInfo addressInfo = gson.fromJson(result, AddressInfo.class);
                        addressInfoList = addressInfo.getInfo();
                        recyclerView.setVisibility(View.VISIBLE);
                        empty_layout.setVisibility(View.GONE);
                        initItemRectclerView();

                    } else if (jsonObject.optString("re").equals("0")) {
                        recyclerView.setVisibility(View.GONE);
                        empty_layout.setVisibility(View.VISIBLE);
                        tv_msg.setText("暂无收货地址~");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }
        });
    }

    @Override
    public void editClick(int position) {
        //编辑按钮
        final AddressInfo.InfoBean infoBean = addressInfoList.get(position);
        startActivity(EditAddressActivity.createIntent(AddressActivity.this, infoBean));
    }
    @Override
    public void MOrenClick(int position) {
        initAddressList();
    }
}
